/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.raflik.queryxml.ui.tasks;

import pl.raflik.queryxml.ui.tasks.io.PreviewQueryInput;
import pl.raflik.queryxml.ui.tasks.io.PreviewQueryOutput;
import pl.raflik.queryxml.ui.tasks.io.CommonQueryOutput;
import java.util.Map;
import org.w3c.dom.Document;
import pl.raflik.queryxml.ui.UITransfer;

/**
 *
 * @author raflik
 */
public class PreviewQueryThread extends CommonQueryThread<PreviewQueryInput, PreviewQueryOutput> {

    public PreviewQueryThread(UITransfer<PreviewQueryInput, PreviewQueryOutput> transfer, Map<String, Document> cache) {
        super(transfer, cache);
    }

    @Override
    protected PreviewQueryOutput performOperation(PreviewQueryInput input) {
        CommonQueryOutput output = validateQuery(input);
        QueryResultCode result = output.getResultCode();
        if (result.isError()) {
            return new PreviewQueryOutput(result);
        }

        long before = System.currentTimeMillis();

        output = invokeQueryUnchecked(input);

        long after = System.currentTimeMillis();
        long processingTime = after - before;

        return new PreviewQueryOutput(output.getResultCode(), output.getQueryResult(), processingTime);
    }
}
